package com.lin.mr.maizilaundry.fragmentmain;

import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.baoyz.actionsheet.ActionSheet;
import com.lin.mr.maizilaundry.R;
import com.lin.mr.maizilaundry.activity.CustomSwitchActivity;
import com.lin.mr.maizilaundry.activity.FeedBackActivity;
import com.lin.mr.maizilaundry.activity.HomeActivity;
import com.lin.mr.maizilaundry.activity.MapActivity;
import com.lin.mr.maizilaundry.utils.CountEvent;
import com.lin.mr.maizilaundry.utils.ShareUtils;
import com.lin.mr.maizilaundry.view.FragmentMoreView;

import de.greenrobot.event.EventBus;

/**
 * Created by lenovog on 2016/7/6.
 */
public class FragmentMore extends Fragment implements View.OnClickListener, ActionSheet.ActionSheetListener {
    //ActionSheet的安装
    private ImageView iv_back;
    private Button btn_unLogin;
    private FragmentMoreView moreContact;
    private FragmentMoreView moreQuestion;
    private FragmentMoreView moreService;
    private FragmentMoreView moreAbout;
    private FragmentMoreView moreAgreement;
    private FragmentMoreView moreFeedback;

    private SharedPreferences sharedPreferences;
    private SharedPreferences.Editor editor;

    private String name;
    private String phoneNumber;

    private long firstClickTime;//第一次点击“退出登录”按钮的时间

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_more, container, false);
        iv_back = (ImageView) view.findViewById(R.id.iv_back);
        btn_unLogin = (Button) view.findViewById(R.id.btn_unLogin);
        moreContact = (FragmentMoreView) view.findViewById(R.id.more_contact);
        moreQuestion = (FragmentMoreView) view.findViewById(R.id.more_question);
        moreService = (FragmentMoreView) view.findViewById(R.id.more_service);
        moreAbout = (FragmentMoreView) view.findViewById(R.id.more_about);
        moreAgreement = (FragmentMoreView) view.findViewById(R.id.more_agreement);
        moreFeedback = (FragmentMoreView) view.findViewById(R.id.more_feedback);

        iv_back.setOnClickListener(this);
        moreContact.setOnClickListener(this);
        moreService.setOnClickListener(this);
        moreFeedback.setOnClickListener(this);
        btn_unLogin.setOnClickListener(this);
        moreAgreement.setOnClickListener(this);

        phoneNumber = ShareUtils.getNumber(getContext());
        if (!phoneNumber.equals("")) {
            //如果手机号码不为空，说明用户用验证码登录过
            //注意：这里的设置是只有在登录状态下才会保存信息
            btn_unLogin.setVisibility(View.VISIBLE);
        } else {
            btn_unLogin.setVisibility(View.INVISIBLE);
        }
        return view;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back://返回
                HomeActivity homeActivity = (HomeActivity) getActivity();
                homeActivity.unLoginForBack();
                break;
            case R.id.more_contact://联系客服
                ActionSheet.createBuilder(getActivity(), getActivity().getSupportFragmentManager())
                        .setCancelButtonTitle("取消")
                        .setOtherButtonTitles("您确定要拨打电话？", "客服电话：1380013800")
                        .setCancelableOnTouchOutside(true)//点击空白处关闭
                        .setListener(FragmentMore.this).show();
                break;
            case R.id.more_service://服务范围
                startActivity(new Intent(getActivity(), MapActivity.class));
                break;
            case R.id.more_feedback://意见反馈
                startActivity(new Intent(getActivity(), FeedBackActivity.class));
                break;
            case R.id.more_agreement://用户协议
                startActivity(new Intent(getActivity(), CustomSwitchActivity.class));
                break;
            case R.id.btn_unLogin://双击退出登录
                //双击时间的简便写法，参考Android源码
//                long[] mHits = new long[2];// 数组长度表示要点击的次数
//                System.arraycopy(mHits, 1, mHits, 0, mHits.length - 1);
//                mHits[mHits.length - 1] = SystemClock.uptimeMillis();// 开机后开始计算的时间
//                if (mHits[0] >= (SystemClock.uptimeMillis() - 500)) {
//
//                    ShareUtils.removeName(getContext());
//                    ShareUtils.removeAddress(getContext());
//                    ShareUtils.removeCity(getContext());
//                    ShareUtils.removeGender(getContext());
//                    ShareUtils.removeNumber(getContext());
//
//                    Toast.makeText(getContext(), "你已退出登录", Toast.LENGTH_SHORT).show();
//                    EventBus.getDefault().post(new CountEvent(3));
//                }

                if (firstClickTime > 0) {// 发现之前点击过一次
                    if (System.currentTimeMillis() - firstClickTime < 3000) {
                        // 判断两次点击是否小于3秒，是的话让用户退出登录，移除个人信息
                        Toast.makeText(getContext(), "你已退出登录", Toast.LENGTH_SHORT).show();
                        firstClickTime = 0;//重置时间, 重新开始
                        btn_unLogin.setVisibility(View.INVISIBLE);
                        ShareUtils.removeName(getContext());
                        ShareUtils.removeAddress(getContext());
                        ShareUtils.removeCity(getContext());
                        ShareUtils.removeGender(getContext());
                        ShareUtils.removeNumber(getContext());
                        EventBus.getDefault().post(new CountEvent(3));
                        return;
                    } else {
                        //用户虽然之前点击过一次，但是再次点击时超过了3秒，故再提示一次
                        Toast.makeText(getContext(), "再点击一次退出登录", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(getContext(), "再点击一次退出登录", Toast.LENGTH_SHORT).show();

                }

                firstClickTime = System.currentTimeMillis();

                break;
        }
    }

    @Override
    public void onDismiss(ActionSheet actionSheet, boolean isCancel) {

    }

    @Override
    public void onOtherButtonClick(ActionSheet actionSheet, int index) {
        if (index == 0) { //判断用户点击的是菜单上的哪一个按钮
            Intent intent = new Intent(Intent.ACTION_CALL);
            intent.setData(Uri.parse("tel:1380013800"));
            startActivity(intent);
        }
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        if (isVisibleToUser) {
//            Log.e("Tag","FragmentMore中的setUserVisibleHint");
//            phoneNumber = ShareUtils.getNumber(MyApplication.getContext());
//            if (!phoneNumber.equals("")) {
//                //如果手机号码不为空，说明用户用验证码登录过
//                //注意：这里的设置是只有在登录状态下才会保存信息
//                btn_unLogin.setVisibility(View.VISIBLE);
//            } else {
//                btn_unLogin.setVisibility(View.INVISIBLE);
//            }
        }
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
        Log.e("Tag", "onCreate中的getUserInfo()");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    public void onEventMainThread(CountEvent event) {
        int count = event.getCount();
        if (count == 7) {
            phoneNumber = ShareUtils.getNumber(getContext());
            if (!phoneNumber.equals("")) {
                //如果手机号码不为空，说明用户用验证码登录过
                //注意：这里的设置是只有在登录状态下才会保存信息
                btn_unLogin.setVisibility(View.VISIBLE);
            } else {
                btn_unLogin.setVisibility(View.GONE);
            }
            Log.e("EventBus", "FragmentMore中的EventBus");
        }
    }
}
